package com.jsgt.greekfire.ui.market.fragement

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE
import com.fhx.core.base.BaseFragment
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.DepthBean
import com.jsgt.greekfire.ui.market.adapter.DepthAdapter
import kotlinx.android.synthetic.main.fragment_depth.*

private const val ARG_PARAM1 = "param1"

class DepthFragment : BaseFragment() {
    private var symbol: String? = null
    var depthBean: DepthBean? = null
    private val depthAdapter: DepthAdapter by lazy { DepthAdapter() }
    private var isScroll = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            symbol = it.getString(ARG_PARAM1)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_depth
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String) =
            DepthFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                }
            }
    }

    override fun main() {
        val symbolArray = symbol?.split("_")
        if (symbolArray != null && symbolArray.size > 1) {
            tvNumber.text = "${resources.getText(R.string.number)}(${symbolArray[0].toUpperCase()})"
            tvPrice.text = "${resources.getText(R.string.price)}(${symbolArray[1].toUpperCase()})"
            tvNumberR.text = "${resources.getText(R.string.number)}(${symbolArray[0].toUpperCase()})"
        } else {
            tvNumber.text = resources.getText(R.string.number)
            tvPrice.text = resources.getText(R.string.price)
            tvNumberR.text = resources.getText(R.string.number)
        }
//        rvDepth.isNestedScrollingEnabled = false
        rvDepth.layoutManager = LinearLayoutManager(context)
        val dm = resources.displayMetrics
        val width = dm.widthPixels / 2
        depthAdapter.setWidth(width)
        rvDepth.adapter = depthAdapter

        //记录滑动状态，如果滑动中不需要刷新页面，静止时立即刷新
        rvDepth.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == SCROLL_STATE_IDLE) {
                    isScroll = false
                    if (depthBean != null) {
                        depthAdapter.setData(depthBean!!)
                    }
                } else {
                    isScroll = true
                }
            }
        })

        if (depthBean != null) {
            depthAdapter.setData(depthBean!!)
        }
    }

    fun setData(depthBean: DepthBean?) {
        if (depthBean == null){
            return
        }
        this.depthBean = depthBean
        if (!isScroll) {
            depthAdapter.setData(depthBean)
        }
    }

    fun setDataNoRefresh(depthBean: DepthBean?) {
        if (depthBean == null){
            return
        }
        this.depthBean = depthBean
    }

    fun refresh() {
        if (depthBean != null) {
            depthAdapter.setData(depthBean!!)
        }
    }
}